package cn.tworice.client.web;

import cn.tworice.client.service.PortService;
import cn.tworice.common.entity.code.*;
import cn.tworice.common.inducts.InductsListener;
import cn.tworice.common.vo.*;
import com.alibaba.excel.EasyExcel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.List;

@RestController
@RequestMapping("/client/port")
@CrossOrigin
public class PortClient {
    @Autowired
    private PortService service;

    @GetMapping("list")
    public RequestResult list(Integer page, Integer pageSize, Port entity) {
        RequestResult result = new RequestResult();
        List<Port> list = service.getList(page, pageSize, entity);
        int total = service.getTotal(entity);
        result.appendData("list", list);
        result.appendData("total", total);
        return result;
    }

    @PostMapping("add")
    public RequestResult add(Port entity) {
        RequestResult result = new RequestResult();
        service.set(entity);
        return result;
    }

    @DeleteMapping("del")
    public RequestResult del(Integer[] ids) {
        RequestResult result = new RequestResult();
        service.del(ids);
        return result;
    }

    @PostMapping("inducts")
    public RequestResult inducts(MultipartFile file) {
        RequestResult result = new RequestResult();
        try {
            InputStream inputStream = file.getInputStream();
            EasyExcel.read(inputStream, Port.class, new InductsListener(service)).sheet().doRead();
            result.setStatus(new StatusCode(200, "批量导入数据成功")
            );
        } catch (Exception ex) {
            ex.printStackTrace();
            result.setStatus(new StatusCode(204, "批量导入数据失败")
            );
        }
        return result;
    }
}